// pages/my/setting/setting.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    staticUrl: app.globalData.staticUrl,
      userInfo:{},
      pl_photo:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
   this.password = this.selectComponent('#password')
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
     var isautologin =  wx.getStorageSync("isautologin");
     if(isautologin){
        this.setData({isautologin:isautologin});
     }
     else{
      this.setData({isautologin:false});
     }
     this.setData({userInfo:app.globalData.userInfo});
     var userInfo = this.data.userInfo;
     var pl_photo = userInfo.pl_photo?userInfo.pl_photo:userInfo.avatarUrl;
     this.setData({pl_photo:pl_photo});
     console.log(this.data.userInfo);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  
  showPopup:function(){
    this.password.showPopup();
  },
  _error:function(){
    this.password.hidePopup();
  },
  _success:function(e){
    var password = e.detail;
    var that = this;
    wx.showLoading({
      title: '正在修改密码',
    })
    app.updatepassword(password,function(res){
       that._error();    
      //跳转到成功页面
          var succuss = {
            message: "密码修改成功！",
            time:new Date().toLocaleDateString() + " " + new Date().toLocaleTimeString()  ,
            back:true,
            buttoninfo: "返回"
          }

          wx.navigateTo({
        
            url: '/pages/succuss/index?succuss=' + JSON.stringify(succuss)

          })
    },function(){
        wx.hideLoading();
    })
  },
  switchautologin:function(){
      this.setData({isautologin:!this.data.isautologin});
      console.log(this.data.isautologin);
      wx.setStorageSync('isautologin', this.data.isautologin);
      console.log( wx.getStorageSync("isautologin"));
  },

  changepic:function(){
    var that = this;
    app.changeplayerpic(this.data.userInfo.player_id,function(res){
          if(res.data){
            console.log(res.data);
            app.globalData.userInfo.pl_photo = res.data;
            var pl_photo =res.data +"?rand="+Math.random();
            console.log(app.globalData.userInfo);
            that.setData({userInfo:app.globalData.userInfo,
              pl_photo:pl_photo});
            var isautologin =  wx.getStorageSync("isautologin");
            if(isautologin){
              wx.setStorageSync('userInfo', app.globalData.userInfo);
            }
          
            
          }
    },function(){});

  },
})